import platgo as pg
import numpy as np

"""
%------------------------------- Reference --------------------------------
% C. A. Coello Coello and M. S. Lechuga, MOPSO: A proposal for multiple
% objective particle swarm optimization, Proceedings of the IEEE Congress
% on Evolutionary Computation, 2002, 1051-1056.
"""


def Operator_PSO(pop: pg.Population, pbest: pg.Population, gbest_dec: np.ndarray, w: float = None) -> pg.Population:
    """
    :param pop:           原始种群
    :param pbest:         记录个体最优的种群
    :param gbest_dec:     全局最优的决策变量
    :param w:             惯性权重
    :return: pop
    """
    if w is None:
        w = .4

    pbest_dec = pbest.decs
    N, D = pop.decs.shape
    if pop.vel is None:
        pop.vel = np.zeros((N, D))

    r1 = np.random.random((N, 1))
    r2 = np.random.random((N, 1))
    offvel = w * pop.vel + r1 * (pbest_dec - pop.decs) + r2 * (gbest_dec - pop.decs)
    offdec = pop.decs + offvel
    offspring = pg.Population(decs=offdec, vel=offvel)

    return offspring
